package com.lzp.controller;

import com.lzp.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: Thymeleaf API
 * @author: lzp
 * @createTime: 2021-07-27 00:12
 **/
@Controller
@RequestMapping("th")
public class ThymeleafController {


    @RequestMapping("/index")
    public String index(ModelMap modelMap) {
        modelMap.addAttribute("name", "lzp");
        return "thymeleaf/index";
    }

    @RequestMapping("center")
    public String center() {
        return "thymeleaf/center/center";
    }

    /**
     * 测试Thymeleaf的一些用法
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("test")
    public String test(ModelMap modelMap) {
        User user = new User();
        user.setName("lzp");
        user.setBirthday(new Date());
        user.setPassword("123456");
        user.setAge(18);
        user.setDesc("<font color='green'><b>hello thymeleaf</b></font>");

        modelMap.addAttribute("user", user);

        User u1 = new User();
        u1.setAge(19);
        u1.setName("imooc");
        u1.setPassword("123456");
        u1.setBirthday(new Date());

        User u2 = new User();
        u2.setAge(17);
        u2.setName("LeeCX");
        u2.setPassword("123456");
        u2.setBirthday(new Date());

        List<User> userList = new ArrayList<>();
        userList.add(user);
        userList.add(u1);
        userList.add(u2);

        modelMap.addAttribute("userList", userList);

        return "thymeleaf/test";
    }

    @PostMapping("postform")
    public String postform(User user) {
        System.out.println("姓名：" + user.getName());
        System.out.println("年龄：" + user.getAge());

        // 重定向
        return "redirect:/th/test";
    }
}
